package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.LoginBean;
import com.mingmen.canting.model.QueRenLingQuModel;

import java.util.List;

/**
 * Contact联系人适配器
 *
 * @author nanchen
 * @fileName WaveSideBarView
 * @packageName com.nanchen.wavesidebarview
 * @date 2016/12/27  15:33
 * @github https://github.com/nanchen2251
 */

public class QueRenlingAdapter extends RecyclerView.Adapter<QueRenlingAdapter.ContactsViewHolder> {

    private List<QueRenLingQuModel.DataBean> contacts;
    private static final String TAG = "ContactsAdapter";
    private OnitemClick onitemClick;   //定义点击事件接口
    Context context;

    public QueRenlingAdapter(Context context, List<QueRenLingQuModel.DataBean> contacts) {
        this.contacts = contacts;
        this.context = context;
    }

    public void setDataList(List<QueRenLingQuModel.DataBean> contacts) {
        this.contacts = contacts;
        notifyDataSetChanged();
    }

    //定义设置点击事件监听的方法
    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.layaout_item_querenling, null);
        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, int position) {
        QueRenLingQuModel.DataBean querenBean = contacts.get(position);
        if (onitemClick != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //在TextView的地方进行监听点击事件，并且实现接口

                }
            });
            holder.text_cangku.setText(querenBean.getCangku());
            holder.text_cangku.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onitemClick.onItemClick(v, position, contacts);
                }
            });
            holder.text_submitcangku.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onitemClick.onItemClick(v, position, contacts);
                }
            });

        }
        holder.text_spec.setText(querenBean.getSpecs());
        holder.tvName.setText(querenBean.getCommodityName());
        holder.text_mark.setText(querenBean.getMark());
        holder.commodityCount.setText("X" + querenBean.getCommodityCount() + "");
        if (position == contacts.size() - 1) {
            holder.view_line.setVisibility(View.GONE);
        } else {
            holder.view_line.setVisibility(View.VISIBLE);
        }
        Glide.with(context).load(AppConfig.IMGURL + querenBean.getPhoto()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar);
    }


    @Override
    public int getItemCount() {
        return contacts.size();
    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {


        TextView tvName;
        TextView text_cangku;
        TextView text_submitcangku;
        TextView text_mark;
        TextView commodityCount;
        ImageView ivAvatar;
        TextView text_spec;
        View view_line;

        ContactsViewHolder(View itemView) {
            super(itemView);

            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            text_cangku = itemView.findViewById(R.id.text_cangku);
            text_submitcangku = itemView.findViewById(R.id.text_submitcangku);
            text_mark = itemView.findViewById(R.id.text_mark);
            commodityCount = itemView.findViewById(R.id.commodityCount);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            text_spec = itemView.findViewById(R.id.text_spec);
            view_line = itemView.findViewById(R.id.view_line);
        }
    }

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int position, List<QueRenLingQuModel.DataBean> contacts);
    }
}
